package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.model.entity.ApartmentInfo;
import com.atguigu.lease.model.entity.LabelInfo;
import com.atguigu.lease.model.entity.ViewAppointment;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.app.mapper.GraphInfoMapper;
import com.atguigu.lease.web.app.mapper.LabelInfoMapper;
import com.atguigu.lease.web.app.mapper.RoomInfoMapper;
import com.atguigu.lease.web.app.mapper.ViewAppointmentMapper;
import com.atguigu.lease.web.app.service.ApartmentInfoService;
import com.atguigu.lease.web.app.service.ViewAppointmentService;
import com.atguigu.lease.web.app.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.app.vo.appointment.AppointmentDetailVo;
import com.atguigu.lease.web.app.vo.appointment.AppointmentItemVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【view_appointment(预约看房信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
public class ViewAppointmentServiceImpl extends ServiceImpl<ViewAppointmentMapper, ViewAppointment>
        implements ViewAppointmentService {
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private ApartmentInfoService apartmentInfoService;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private RoomInfoMapper roomInfoMapper;


    /**
     * @author gengjian
     * @date 2024/12/16 11:04:46
     * 查询个人预约看房列表
     */
    @Override
    public List<AppointmentItemVo> listAppointmentItemVo(Long userId) {
        //根据登录用户id获取预约看房基本信息集合
        LambdaQueryWrapper<ViewAppointment> viewAppointmentLambdaQueryWrapper = new LambdaQueryWrapper<>();
        viewAppointmentLambdaQueryWrapper.eq(ViewAppointment::getUserId,userId);
        List<ViewAppointment> viewAppointmentList = super.list(viewAppointmentLambdaQueryWrapper);
        //创建AppointmentItemVo集合
        List<AppointmentItemVo> appointmentItemVoList = new ArrayList<>();
        //循环遍历基本信息集合
        for (ViewAppointment viewAppointment : viewAppointmentList) {
            //根据公寓id获取公寓名称
            ApartmentInfo apartmentInfo = apartmentInfoService.getById(viewAppointment.getApartmentId());
            String apartmentName = apartmentInfo.getName();
            //根据公寓id获取公寓图片列表
            List<GraphVo> graphVoList = graphInfoMapper.selectListGraphVoByItemTypeAndId(ItemType.APARTMENT, viewAppointment.getApartmentId());
            //创建AppointmentItemVo
            AppointmentItemVo appointmentItemVo = new AppointmentItemVo();
            //封装vo
            BeanUtils.copyProperties(viewAppointment,appointmentItemVo);
            appointmentItemVo.setApartmentName(apartmentName);
            appointmentItemVo.setGraphVoList(graphVoList);
            //将vo添加到集合中
            appointmentItemVoList.add(appointmentItemVo);
        }
        //返回结果
        return appointmentItemVoList;
    }

    /**
     * @author gengjian
     * @date 2024/12/16 11:20:31
     * 根据预约ID查询预约详情信息
     */
    @Override
    public AppointmentDetailVo getAppointmentDetailVoById(Long id) {
        //获取预约看房基本信息
        ViewAppointment viewAppointment = super.getById(id);
        //根据公寓id获取公寓基本信息
        ApartmentInfo apartmentInfo = apartmentInfoService.getById(viewAppointment.getApartmentId());
        //创建apartmentItemVo
        ApartmentItemVo apartmentItemVo = new ApartmentItemVo();
        //根据公寓id获取公寓图片列表
        List<GraphVo> graphVoList = graphInfoMapper.selectListGraphVoByItemTypeAndId(ItemType.APARTMENT, viewAppointment.getApartmentId());
        //根据公寓id获取公寓标签
        List<LabelInfo> labelInfoList = labelInfoMapper.selectListLabelInfoByApartmentId(viewAppointment.getApartmentId());
        //根据公寓id查询房间信息最低租金
        BigDecimal minRent = roomInfoMapper.selectMinRentByApartmentId(viewAppointment.getApartmentId());
        //创建AppointmentDetailVo对象
        AppointmentDetailVo appointmentDetailVo = new AppointmentDetailVo();
        //封装
        apartmentItemVo.setGraphVoList(graphVoList);
        apartmentItemVo.setLabelInfoList(labelInfoList);
        apartmentItemVo.setMinRent(minRent);
        BeanUtils.copyProperties(apartmentInfo,apartmentItemVo);
        appointmentDetailVo.setApartmentItemVo(apartmentItemVo);
        BeanUtils.copyProperties(viewAppointment,appointmentDetailVo);
        //返回结果
        return appointmentDetailVo;
    }
}




